package prolog.fluents;
import prolog.terms._
import java.util.{ArrayList => v1}
import prolog.terms.Prog;
import prolog.terms.{Source => s1};
import prolog.terms.Term;

/**
  An Infinite Source.
  If based on a finite Source, it moves to its the first element after reaching its last element.
  A SourceLoop returns 'no' if the original Source is empty. 
  In case the original Source is infinite, a SourceLoop will 
  return the same elements as the original Source. 
  (In particular, this happens if the original Source is also a Source loop).
*/
class SourceLoop1(s:s1, i:Int, v:v1) extends s1,v1{
  private var v:v1;
  s:s1;  
  protected var i:Int;
   
 def this(s:s1, i:Int, v:v1) = this(p,s1,v = new ArrayList(),i=0)
  
  private final def getMemoized():Term ={
    if(null == v || v.size()<=0)
      return null;
    Term T=(Term)v.get(i);
    i=(i+1)%v.size();
    T;
      }
  
   override def getElement():Term = {
    T = null;
    if(null!= s1) { 
      T = s1.getelement();
      if(null!= T)
        v.add(T);
      else {
        s1 = null;
           }
         }
    if(null==s1){
      T = getMemoized();
    return T;
      }
   }
  
 override def stop():Unit = {
    v=null;
    s1=null;
  }
}
